package com.jyuxuan.recruitment.controller;

import com.jyuxuan.recruitment.pojo.Demand;
import com.jyuxuan.recruitment.pojo.EnterpriseLogin;
import com.jyuxuan.recruitment.service.impl.DemandServiceImpl;
import com.jyuxuan.recruitment.utils.DataMap;
import com.jyuxuan.recruitment.utils.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin
@RequestMapping("/demand")
public class DemandController {

    @Autowired
    private DemandServiceImpl demandService;


    /**
     * 企业查询自身需求信息
     *
     * @return
     */
    @PostMapping("viewDemand")
    @ResponseBody
    public String viewDemand() {
        EnterpriseLogin enterpriseLogin = (EnterpriseLogin) SecurityUtils.getSubject().getPrincipal();
        String number = enterpriseLogin.getNumber();
        DataMap data = demandService.demandsView(number);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 管理员查询需求信息
     *
     * @return
     */
    @PostMapping("viewAllDemand")
    @ResponseBody
    public String viewAllDemand(Demand demand) {
        DataMap data = demandService.demandsAllView(demand);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 审核需求
     *
     * @param demand
     * @return
     */
    @PostMapping("auditDemand")
    @ResponseBody
    public String auditDemand(Demand demand) {
        DataMap data = demandService.demandAudit(demand);
        return JsonResult.build(data).toJSON();
    }


    /**
     * 上传企业用人需求
     *
     * @param demand
     * @return
     */
    @PostMapping("submitDemand")
    @ResponseBody
    public String submitDemand(Demand demand) {
        EnterpriseLogin enterpriseLogin = (EnterpriseLogin) SecurityUtils.getSubject().getPrincipal();
        String number = enterpriseLogin.getNumber();
        demand.setState(0);
        DataMap data = demandService.demandInsert(demand, number);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 修改企业用人需求
     *
     * @param demand
     * @return
     */
    @PostMapping("modifyDemand")
    @ResponseBody
    public String modifyDemand(Demand demand) {
        demand.setState(0);
        DataMap data = demandService.demandModify(demand);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 删除企业用人需求
     *
     * @param demand
     * @return
     */
    @PostMapping("deleteDemand")
    @ResponseBody
    public String deleteDemand(Demand demand) {
        DataMap data = demandService.demandDelete(demand);
        return JsonResult.build(data).toJSON();
    }


}

